package com.project.domain.dto.projectCheckDto;

import lombok.Data;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
import java.util.ArrayList;

/**
 * Created by cw13031$ on 2025/2/26.
 * Description:
 */
@Data
public class SearchReqDto {
    private String name;//项目名称
    private String applierName;
    private BigDecimal minFee;
    private BigDecimal maxFee;
//    private String applyDate;//
    private String applyDate1;//
    private String applyDate2;//
    private ArrayList<String> lixiangState;//数组
    private ArrayList<String> unitId;//数组
    private String projectCode;//项目编号
    private ArrayList<String> l1Code;
    private String keyWord;
    private String checkState;
    private ArrayList<String> checkStateList;
//    // 转换后的开始和结束日期
//    private LocalDate startDate;
//    private LocalDate endDate;
    private String infoId;

    private Integer pageSize;
    private Integer pageNum;
    private String orderBy;

    /* 由于页面样式修改。原本输入 -2020可表示2020年之前
    现在改为两个日期输入框，此处不再需要转换
    // setter 方法触发转换
    public void setApplyDate(String applyDate) {
        this.applyDate = applyDate;
        convertDateRange(); // 每次设置 applyDate 时自动转换
    }

    // 转换逻辑
    private void convertDateRange() {
        if (this.applyDate == null || this.applyDate.trim().isEmpty()) {
            this.startDate = null;
            this.endDate = null;
            return;
        }

        String trimmed = this.applyDate.trim();
        try {
            if (trimmed.matches("\\d{4}")) { // 单个年份，如 "2018"
                int year = Integer.parseInt(trimmed);
                this.startDate = LocalDate.of(year, 1, 1); // 年初
                this.endDate = LocalDate.of(year, 12, 31); // 年末
            } else if (trimmed.matches("\\d{4}-")) { // "2014-" 表示 2014 及以后
                int year = Integer.parseInt(trimmed.substring(0, 4));
                this.startDate = LocalDate.of(year, 1, 1); // 年初
                this.endDate = null; // 无结束日期
            } else if (trimmed.matches("-\\d{4}")) { // "-2018" 表示 2018 及以前
                int year = Integer.parseInt(trimmed.substring(1));
                this.startDate = null; // 无开始日期
                this.endDate = LocalDate.of(year, 12, 31); // 年末
            } else if (trimmed.matches("\\d{4}-\\d{4}")) { // "2014-2016" 表示范围
                String[] years = trimmed.split("-");
                int startYear = Integer.parseInt(years[0]);
                int endYear = Integer.parseInt(years[1]);
                if (startYear > endYear) {
                    throw new IllegalArgumentException("开始年份不能大于结束年份");
                }
                this.startDate = LocalDate.of(startYear, 1, 1); // 年初
                this.endDate = LocalDate.of(endYear, 12, 31); // 年末
            } else if (trimmed.matches("\\d{4}-\\d{2}-\\d{2}")) { // 完整日期，如 "2023-05-15"
                this.startDate = LocalDate.parse(trimmed, DateTimeFormatter.ISO_LOCAL_DATE);
                this.endDate = this.startDate; // 单天
            } else if (trimmed.matches("\\d{4}-\\d{2}-\\d{2}-\\d{4}-\\d{2}-\\d{2}")) { // "2023-05-15-2023-06-20"
                String[] dates = trimmed.split("-");
                this.startDate = LocalDate.parse(dates[0] + "-" + dates[1] + "-" + dates[2], DateTimeFormatter.ISO_LOCAL_DATE);
                this.endDate = LocalDate.parse(dates[3] + "-" + dates[4] + "-" + dates[5], DateTimeFormatter.ISO_LOCAL_DATE);
                if (this.startDate.isAfter(this.endDate)) {
                    throw new IllegalArgumentException("开始日期不能晚于结束日期");
                }
            } else {
                throw new IllegalArgumentException("无效的日期格式");
            }
        } catch (DateTimeParseException | NumberFormatException e) {
            throw new IllegalArgumentException("日期或年份必须是有效的格式");
        }
    }

     */
}
